/* Filename: 5sec_sig.c
 * Description:
 * Last modified: humble 20200329 00:04
 */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>

//volatile 意思是告诉编译器去到变量真正地址空间处取数据，不要轻信内存中对这个数值的存放
static volatile int loop = 1;

static void usage(void);

static void alrm_handler(int s)
{
    loop = 0;
}

int main(int argc, char **argv)
{
    int64_t count = 0;

    if(argc != 1){
        fprintf(stderr, "argc !=1");
        usage();
        exit(1);
    }

    signal(SIGALRM, alrm_handler);
    alarm(5); //alarm要在signal之后

    while(loop){
        count++;
    }

    printf("%ld\n", count);
    return 0;
}

static void usage(void)
{
#define USAGE "Usage:\n\
  Helloworld\n"
    printf(USAGE);
}
